
package com.kugou.game.sdk.protocol;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.core.VipManager;
import com.kugou.game.sdk.core.VipManager.GSInfo;
import com.kugou.game.sdk.core.VipManager.VipInfo;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

/**
 * 描述:Vip信息请求
 *
 * @author wcj
 * @since 2016年1月18日 上午10:25:04
 */
public class VipTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    /**
     * @param onlyRequestVipInfo 是否单独请求vip基本信息
     * @return
     */
    public VipResponse request(boolean onlyRequestVipInfo, String userName) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        // 只请求vip信息时，不请求vip专题和vip GS群是不变数据，不需要重复请求
        if (onlyRequestVipInfo) {
            HashMap<String, Object> vipParams = new HashMap<String, Object>();
            vipParams.put("cmd", ProtocolCmd.CMD_VIP_INFO);
            requestParams.add(vipParams);
            mCmdList.add(ProtocolCmd.CMD_VIP_INFO);

        } else {
            HashMap<String, Object> vipADParams = new HashMap<String, Object>();
            vipADParams.put("cmd", ProtocolCmd.CMD_VIP_AD_INFO);
            requestParams.add(vipADParams);

            HashMap<String, Object> vipGSParams = new HashMap<String, Object>();
            vipGSParams.put("cmd", ProtocolCmd.CMD_VIP_GS_INFO);
            requestParams.add(vipGSParams);

            mCmdList.add(ProtocolCmd.CMD_VIP_AD_INFO);
            mCmdList.add(ProtocolCmd.CMD_VIP_GS_INFO);
        }

        // 生成请求包
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        // 生成响应包
        ResponsePackage responsePackage = new HttpResponsePackage();
        // 生成响应类
        VipResponse appsHttpResponse = new VipResponse();

        requestPackage.setRequestParams(requestParams);
        requestPackage.setUserName(userName);

        try {
            KGHttpClient.request(requestPackage, responsePackage);
            // 响应类赋值
            responsePackage.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    private class HttpResponsePackage extends HttpBaseResponsePackage<VipResponse> {

        @Override
        protected void handleResponse(VipResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "VipTask:" + jsonStr);
                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();

                VipInfo vipInfo = VipManager.getInstance().getVipInfo();

                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);

                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }

                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_VIP_INFO.equalsIgnoreCase(cmd) && response.isUseful()) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        vipInfo.setVip(jsonResult.optInt("isvip") == 1);
                        JSONArray gsinfos = jsonResult.optJSONArray("gsinfo");
                        vipInfo.getGsInfos().clear();

                        for (int j = 0; j < gsinfos.length(); j++) {
                            JSONObject infoObject = gsinfos.optJSONObject(j);
                            GSInfo info = new GSInfo();
                            info.setName(infoObject.optString("gsname"));
                            info.setQq(infoObject.optString("gsqq"));
                            info.setImgUrl(infoObject.optString("gsimage"));
                            vipInfo.getGsInfos().add(info);
                        }

                    } else if (ProtocolCmd.CMD_VIP_AD_INFO.equalsIgnoreCase(cmd)
                            && response.isUseful()) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        vipInfo.setVipAdImgUrl(jsonResult.optString("vipad"));
                        vipInfo.setVipTopicUrl(jsonResult.optString("topicurl"));
                        vipInfo.setVipTopicTitle(jsonResult.optString("topictitle"));

                    } else if (ProtocolCmd.CMD_VIP_GS_INFO.equalsIgnoreCase(cmd)
                            && response.isUseful()) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        vipInfo.setGsImageUrlPortait(jsonResult.optString("gsimage"));
                        vipInfo.setGsImageUrlLandscape(jsonResult.optString("gsimage2"));
                        vipInfo.setGsQQ(jsonResult.optString("gsqq"));
                    }

                    if (i == responseArray.length() - 1) {
                        response.setOk(true);
                    }
                }
                response.setVipInfo(vipInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class VipResponse extends ResponseMessage {
        private static final long serialVersionUID = -5905989827340308810L;

        VipManager.VipInfo vipInfo;

        public VipManager.VipInfo getVipInfo() {
            return vipInfo;
        }

        public void setVipInfo(VipManager.VipInfo vipInfo) {
            this.vipInfo = vipInfo;
        }

    }

}
